//
//  CYCarAppealVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/3/1.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCarAppealVC.h"
#import "CYDiscoveryHttpTool.h"
#import "MBProgressHUD+LE.h"
#import "CYCompanyUserTool.h"
#import "CYSimpleLoanM.h"
#import "CYLoginHttpTool.h"

@interface CYCarAppealVC ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *appealTV;
@property (weak, nonatomic) IBOutlet UIButton *keyboardBtn;

@property(nonatomic, copy) CYCompletion successed;
@end

@implementation CYCarAppealVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.appealTV.layer.borderColor = CYNaviThemeColor.CGColor;
    self.appealTV.layer.borderWidth = 1;
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 5;
    self.view.width = CYScreeSize.width - 40;
    self.view.height = 300;
    self.appealTV.delegate = self;
}

- (void)appealSuccessed:(CYCompletion)successed {
    self.successed = [successed copy];
}

- (void)submit {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYDiscoveryHttpTool handleAppealWithParam:@{@"type": @"0", @"reason": self.appealTV.text, @"applyId": self.model.applyId, @"userId": user.userId, @"token": user.token} success:^(NSString *message) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showSuccess:message];
            if (self.successed) {
                self.successed();
                self.successed = nil;
            }
        } failure:^(NSString *message, BOOL isLogin) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showError:message];
            if (!isLogin) {
                [self.navigationController popToRootViewControllerAnimated:NO];
                [CYCompanyUserTool logoutWithController:self];
            }
        }];
    }
}

- (IBAction)confirmBtn:(UIButton *)sender {
    [self.appealTV resignFirstResponder];
    if (self.appealTV && !self.appealTV.text.isEmpty) {
        [self dismissViewControllerAnimated:YES completion:^{
            [self submit];
        }];
    } else {
        [MBProgressHUD showError:@"申诉原因不能为空"];
    }
}
- (IBAction)cancelBtn:(UIButton *)sender {
    [self.appealTV resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)keyBoardBtn:(UIButton *)sender {
    self.keyboardBtn.hidden = YES;
    [self.appealTV resignFirstResponder];
    
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    self.keyboardBtn.hidden = NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.keyboardBtn.hidden = YES;
}
@end
